package com.love.cloud.common.core.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 断言
 * @Author sharlot2050@foxmail.com
 * @Date 2018年11月16日
 * @Version 1.00
 */
public class AssertUtil {

    public static void startWith(String content, String prefix, String message){
        if(!content.startsWith(prefix))throw new IllegalArgumentException(message);
    }

    public static void isTrue(boolean b, String message) {
        if(!b) throw new IllegalArgumentException(message);
    }

    public static void isNotNull(Object object, String message){
        if(object instanceof String){
            if(isEmpty(object))throw new IllegalArgumentException(message);
        }
        if(object == null){
            throw new IllegalArgumentException(message);
        }
    }
    public static void isNotEmpty(Collection collection , String message) {
        if (collection == null || collection.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }
    public static boolean isEmpty(Object o) {
        if (null == o) {
            return true;
        }
        if (o instanceof List) {
            List list = (List) o;
            if (list.size() > 0) {
                return false;
            } else {
                return true;
            }
        } else if (o instanceof Object[]) {
            Object[] objs = (Object[]) o;
            if (objs.length > 0) {
                return false;
            } else {
                return true;
            }
        } else if (o instanceof String) {
            if ("".equals(o.toString().trim()) || "null".equals(o.toString().trim())) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }
}
